import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import login from "../page/login/login.vue";
// import index from "../page/index/index.vue";
let index = () => import("../page/index/index.vue")
let register = () => import("../page/register/register.vue")
let qrdd = () => import("../page/qrdd/qrdd.vue")
// import list from "../page/list/list.vue";
let list = () => Promise.resolve(import("../page/list/list.vue"))

let klist = () => import("../page/list/klist.vue")
let address = () => import("../page/address/address.vue")
let xaddress = () => import("../page/Xaddress/xaddress.vue")

// import detail from "../page/detail/detail.vue";
let detail = () => import("../page/detail/detail.vue")
import search from "../page/search/search.vue"
import ksearch from "../page/search/ksearch.vue"


// import home from "../page/home/home.vue";
// import cate from "../page/cate/cate.vue";
// import shop from "../page/shop/shop.vue";
// import mine from "../page/mine/mine.vue";

const routes = [{
    path: "/login",
    component: login,
    meta: {
      title: "手机登录",
      isshow: true
    }
  },
  {
    path: "/register",
    component: register,
    meta: {
      title: "手机注册",
      isshow1: true
    }
  },
  { 
    path: "/search",
    component: search,
    meta: {
      title: "搜索"
    }
  },
  {
    path: "/ksearch",
    component: ksearch,
    meta: {
      title: "搜索"
    }
  },
  {
    path: "/index",
    component: index,
    children: [{
        path: "home",
        component: () => import("../page/home/home.vue"),
        meta: {
          title: "小U商城"
        }
      },
      {
        path: "cate",
        component: () => import("../page/cate/cate.vue"),
        meta: {
          title: "商品分类"
        }
      },
      {
        path: "shop",
        component: () => import("../page/shop/shop.vue"),
        meta: {
          title: "购物车"
        }
      },
      {
        path: "mine",
        component: () => import("../page/mine/mine.vue"),
        meta: {
          title: "会员中心"
        }
      },
      {
        path: "",
        redirect: "home"
      }
    ]
  },
  {
    path: "/list",
    component: list,
    meta: {
      title: "列表",
      isshow2: true
    }
  },
  {
    path: "/klist",
    component: klist,
    meta: {
      title: "列表"
    }
  },
  {
    path: "/qrdd",
    component: qrdd,
    meta: {
      title: "确认订单"
    }
  },
  {
    path: "/address",
    component: address,
    meta: {
      title: "地址管理"
    }
  },
  {
    path: "/xaddress",
    component: xaddress,
    meta: {
      title: "新增地址"
    }
  },
  {
    path: "/detail/:id",
    component: detail,
    meta: {
      title: "详情"
    }
  },
  {
    path: "/payment",
    component: ()=>import("../page/payment/payment.vue"),
    meta: {
      title: "确认支付"
    }
  },
  {
    path: "*",
    redirect: "/login"
  }
]
const router = new VueRouter({
  routes,
  //滚动处理
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return {
        x: 0,
        y: 0
      }
    }
  }
})
//登录拦截
router.beforeEach((to, from,next)=>{
  if(to.path==="/login"||to.path=="/register"){
    next()
    return;
  }
  let islogin=localStorage.getItem("islogin");
  if(islogin){
    next()
    return;
  }
  next("/login")
})
export default router